package com.qzsoft.zhjd.common.importUtils;

import cn.hutool.core.util.ReflectUtil;
import com.qzsoft.zhjd.common.annotation.DecDict;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.RecursiveTask;
import java.util.stream.Collectors;

public class ExcelImportDecDictForkJoinWork<T> extends RecursiveTask<ExcelImportData> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelImportDecDictForkJoinWork.class);
    private boolean concurrentTask = false;
    private Integer critical = 1000;
    private List<T> list;
    private int minIndex = 0;
    private int maxnIndex = 0;
    private Class<?> pojoClass;
    private Boolean translateDict = true;

    public void setTranslateDict(Boolean translateDict) {
        this.translateDict = translateDict;
    }

    public ExcelImportDecDictForkJoinWork(List<T> list, Class<?> obj){
        this.list = list;
        this.pojoClass = obj;
    }

    public ExcelImportDecDictForkJoinWork(List<T> list, Class<?> obj,int min, int max){
        this.list = list;
        this.pojoClass = obj;
        this.minIndex = min;
        this.maxnIndex =  max;
    }

    @Override
    protected ExcelImportData compute() {
        int lenght = list.size();
        if(lenght<=this.critical){
            LOGGER.debug("excel import concurrent task start {} , end {}", this.minIndex, this.maxnIndex);
            return this.readRow();
        }else{
            int middle = lenght / 2;
            ExcelImportDecDictForkJoinWork right = new ExcelImportDecDictForkJoinWork(this.list.stream().limit(middle).collect(Collectors.toList()), this.pojoClass,0,middle);
            right.fork();
            ExcelImportDecDictForkJoinWork left = new ExcelImportDecDictForkJoinWork(this.list.stream().skip(middle+1).collect(Collectors.toList()), this.pojoClass,middle+1,lenght);
            left.fork();
//            result = (ExcelImportResult)right.join();
        }
        return null;
    }

    private ExcelImportData readRow(){
        ExcelImportData result = new ExcelImportData();
        result.setFailList(new ArrayList());
        result.setList(new ArrayList());
        Iterator var2 = list.iterator();

        while(var2.hasNext()) {
            Object record = var2.next();
            if (record != null && translateDict) {
                Field[] var1 = record.getClass().getDeclaredFields();
                int var4 = var1.length;
                for (int var3 = 0; var3 < var4; ++var3) {
                    Field field = var1[var3];
                    if (field.getAnnotation(DecDict.class) != null) {
                        String codeId = ((DecDict) field.getAnnotation(DecDict.class)).dictCodeType();
                        String tableInfo = ((DecDict) field.getAnnotation(DecDict.class)).tableInfo();
                        String descField = field.getName();
                            Object obj = ReflectUtil.getFieldValue(record,field);
                            if (obj != null) {
                                String code = String.valueOf(obj);
                                if (!StringUtils.isEmpty(code) && !StringUtils.isEmpty(codeId)) {
                                    String textValue = "";
                                    textValue = ImportExcelUtil.getCodeId(codeId,code).getCode();
                                    ReflectUtil.setFieldValue(record, descField, textValue);
                                }
                            }
                    }
                }
            }
                result.getList().add(record);
        }
        return result;
    }
}
